package com.shaoshaohuo.app.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.constant.ExtraName;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.SupplyOrder;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.ui.BaseActivity;
import com.shaoshaohuo.app.ui.EvaluationActivity;
import com.shaoshaohuo.app.ui.ec.MyQuoteDetailActivity;
import com.shaoshaohuo.app.ui.ec.SendGoodsEcActivity;
import com.shaoshaohuo.app.utils.StringHelper;

import java.util.List;

public class SupplyOrderListAdapter<T> extends MyBaseAdapter<SupplyOrder> {


	public SupplyOrderListAdapter(Context ctx, List<SupplyOrder> list, boolean flag) {
		super(ctx, list, flag);
	}

	@Override
	public View getView(int pos, View convertView, ViewGroup group) {
		// TextView ;
		// TextView ;
		// TextView ;
		// TextView ;
		// TextView ;
		// TextView ;
		// TextView ;
		ViewHolder holder;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.view_supply_order_item, null);
			holder = new ViewHolder();
			holder.titleText = (TextView) convertView.findViewById(R.id.textview_product_name);
			holder.supplierText = (TextView) convertView.findViewById(R.id.textview_gongyingshang);
			holder.phoneText = (TextView) convertView.findViewById(R.id.textview_phone);
			holder.priceText = (TextView) convertView.findViewById(R.id.textview_price);
			holder.totalText = (TextView) convertView.findViewById(R.id.textview_total);
			holder.timeText = (TextView) convertView.findViewById(R.id.textview_time);
			holder.statusText = (TextView) convertView.findViewById(R.id.textview_status);
			holder.buttonText1 = (TextView) convertView.findViewById(R.id.textview_button1);
			holder.buttonText2 = (TextView) convertView.findViewById(R.id.textview_button2);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final SupplyOrder item = list.get(pos);

		holder.titleText.setText(item.getTitle());
		holder.supplierText.setText(item.getReceivecontact());
		holder.phoneText.setText(item.getRecemobile());
		holder.priceText.setText(item.getPrice() + "元/" + item.getUnit() + "*" + item.getNum() + item.getUnit());
		holder.totalText.setText("合计:"+item.getTotal() + "元");
		holder.timeText.setText(StringHelper.formatDateMinute(item.getCtime()));

		// -1买家取消订单 -2卖家取消订单 0 待确认 1待付款 2待发货 3运输中 4已完成
		switch (Integer.parseInt(item.getStatus())) {
		case -1:
		case -2:
			holder.statusText.setText("订单取消");
			holder.buttonText1.setVisibility(View.GONE);
            holder.buttonText2.setVisibility(View.VISIBLE);
            holder.buttonText2.setText("删除订单");
            holder.buttonText2.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                	showDeleteDialog(item);
                }
            });
			break;
		case 0:
			holder.statusText.setText("已报价");
			holder.buttonText1.setVisibility(View.GONE);
			holder.buttonText2.setVisibility(View.VISIBLE);
			holder.buttonText2.setText("我的报价");
			holder.buttonText2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(context, MyQuoteDetailActivity.class);
					intent.putExtra(ExtraName.orderid, item.getOrderid());
					context.startActivity(intent);
				}
			});
			break;
		case 1:
			holder.statusText.setText("待付款");
			holder.buttonText1.setVisibility(View.GONE);
			holder.buttonText2.setVisibility(View.VISIBLE);
			holder.buttonText2.setText("提醒付款");
			holder.buttonText2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					remindOrder(item, 1);
				}
			});
			break;
		case 2:
			holder.statusText.setText("对方已付款");
			holder.buttonText1.setVisibility(View.VISIBLE);
			holder.buttonText2.setVisibility(View.VISIBLE);
			holder.buttonText1.setText("平台发货");
			holder.buttonText1.setOnClickListener(new OnClickListener() {
			    
			    @Override
			    public void onClick(View v) {
			        Intent intent = new Intent(context, SendGoodsEcActivity.class);
                    intent.putExtra(ExtraName.supplyItem, item);
                    context.startActivity(intent);
			    }
			});
			holder.buttonText2.setText("自主发货");
			holder.buttonText2.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					sendGoodsOrder(item);
				}
			});
			break;
		case 3:
			holder.statusText.setText("货物运输中");
			holder.buttonText1.setVisibility(View.GONE);
			holder.buttonText2.setVisibility(View.VISIBLE);
			holder.buttonText2.setText("提醒对方收货");
			holder.buttonText2.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					remindOrder(item, 2);
				}
			});
			break;
		case 4:
			holder.statusText.setText("交易完成");

			if ("1".equals(item.getSelldisscuz())) {
				holder.buttonText1.setVisibility(View.GONE);
				holder.buttonText2.setVisibility(View.VISIBLE);
				holder.buttonText2.setText("删除订单");
				holder.buttonText2.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						showDeleteDialog(item);
					}
				});
			} else {
				holder.buttonText1.setVisibility(View.VISIBLE);
				holder.buttonText2.setVisibility(View.VISIBLE);
				holder.buttonText1.setText("评价");
				holder.buttonText1.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent intent = new Intent(context, EvaluationActivity.class);
						 intent.putExtra(ExtraName.type, EvaluationActivity.EVALUATION_TYPE_SUPPLY);
						intent.putExtra(ExtraName.orderid, item.getOrderid());
						context.startActivity(intent);
					}
				});
				holder.buttonText2.setText("删除订单");
				holder.buttonText2.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						showDeleteDialog(item);
					}
				});
			}
			break;
		}


		return convertView;
	}

	protected void remindOrder(SupplyOrder item, int typeid) {
		((BaseActivity) context).startLoadingDialog();
		RequestService.getInstance().supplyOrderRemind(context, item.getOrderid(), typeid + "", BaseEntity.class,
				new RequestListener() {

					@Override
					public void onSuccess(int requestCode, BaseEntity resultData) {
						((BaseActivity) context).dismissLoadingDialog();
						if (resultData.isOk()) {
							((BaseActivity) context).showToast("提醒成功");
						} else {
							((BaseActivity) context).showToast(resultData.getMsg());
						}
					}

					@Override
					public void onFailed(int requestCode, Exception error, String msg) {
						((BaseActivity) context).dismissLoadingDialog();
						((BaseActivity) context).showToast(R.string.please_check_network);
					}
				});
	}

	private void showDeleteDialog(final SupplyOrder item) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage("确认删除此订单?");
		builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
				deleteOrder(item);
			}
		});
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

			@Override
			public void onClick(DialogInterface dialog, int which) {
			}
		});
		builder.create().show();
	}

	protected void deleteOrder(final SupplyOrder item) {
		((BaseActivity) context).startLoadingDialog();
		RequestService.getInstance().deleteSupplyOrder(context, item.getOrderid(), BaseEntity.class, new RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				((BaseActivity) context).dismissLoadingDialog();
				if (resultData.isOk()) {
					((BaseActivity) context).showToast("订单删除成功");
					list.remove(item);
					notifyDataSetChanged();
				} else {
					((BaseActivity) context).showToast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				((BaseActivity) context).dismissLoadingDialog();
				((BaseActivity) context).showToast(R.string.please_check_network);
			}
		});
	}

	protected void sendGoodsOrder(final SupplyOrder item) {
		((BaseActivity) context).startLoadingDialog();
		RequestService.getInstance().supplyOrderSendGoods(context, item.getOrderid(), BaseEntity.class, new RequestListener() {

			@Override
			public void onSuccess(int requestCode, BaseEntity resultData) {
				((BaseActivity) context).dismissLoadingDialog();
				if (resultData.isOk()) {
					((BaseActivity) context).showToast("发货成功");
					list.remove(item);
					notifyDataSetChanged();
				} else {
					((BaseActivity) context).showToast(resultData.getMsg());
				}
			}

			@Override
			public void onFailed(int requestCode, Exception error, String msg) {
				((BaseActivity) context).dismissLoadingDialog();
				((BaseActivity) context).showToast(R.string.please_check_network);
			}
		});
	}

	static class ViewHolder {

		TextView titleText;
		TextView supplierText;
		TextView phoneText;
		TextView priceText;
		TextView totalText;
		TextView timeText;
		TextView statusText;

		TextView buttonText1;
		TextView buttonText2;
	}

	/**
	 * 提醒付款
	 */
	private OnClickListener mRemindPaymentListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
	};

	/**
	 * 发货
	 */
	private OnClickListener mSendGoodsListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
	};

	/**
	 * 提醒对方收货
	 */
	private OnClickListener mRemindReceviGoodsListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
	};
	/**
	 * 去评价
	 */
	private OnClickListener mEvaluationListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

		}
	};

}
